home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Monster Media 1996 #15
/
Monster Media Number 15 (Monster Media)(July 1996).ISO
/
prog_c
/
cuj0696.zip
/
DWYER.ZIP
/
SPECTRAL.TST
/
GETCTLDA.CX
< prev
next >
Wrap
Text File
|
1996-03-09
|
6KB
|
228 lines
/* ============ */
/* getctlda.cx */
/* ============ */
#include <io.h>
#include <signal.h>
#include <stdio.h>
#include <xtendefs.h>
#define FLUSH_LINE(u) \
{ \
int x; \
do \
{ \
x = getc(u); \
} \
while (x != EOF && x != '\n'); \
}
/* ==================================================================== */
/* AbortGracefully - does the right thing at <CTRL> C or <CTRL> break */
/* ==================================================================== */
static void
AbortGracefully(int unused)
{
if (unused || !unused) /* Silences Most Compilers */
exit(1);
}
/* ==================================================== */
/* GetStr - Prompts operator with s to get string Str */
/* ==================================================== */
void
GetStr(char *s, char *Str)
{
fprintf(stderr, "%s", s);
scanf("%s", Str);
FLUSH_LINE(stdin);
}
/* ============================================================ */
/* GetShort - Prompts operator with s to get short integer i */
/* ============================================================ */
void
GetShort(char *s, short *i)
{
fprintf(stderr, "%s", s);
scanf("%d", i);
FLUSH_LINE(stdin);
}
/* ==================================================================== */
/* GetControlData - Prompts user for Multiplier(s), Modulus & Dimension */
/* ==================================================================== */
short
GetControlData(USHORT *Multiplier, USHORT *Modulus, short *Dimension,
short *HowMany)
{
short InputOK = TRUE;
short RelPrime;
int NewlineCh;
USHORT Tmp1[NE];
char TmpStr[128];
/* -------------------- */
/* Execute exit() on ^C */
/* -------------------- */
signal(SIGINT, AbortGracefully);
NewlineCh = _isatty(_fileno(stdin)) ? '\r' : '\n';
for (;;)
{
GetShort("How Many Multipliers? [1 or 2]: ", HowMany);
if (*HowMany == 1 || *HowMany == 2)
{
break;
}
fprintf(stderr, "Incorrect Response: %d\n", *HowMany);
}
fflush(NULL);fprintf(stderr, "%c", NewlineCh);
if (*HowMany == 1)
{
for (;;)
{
GetStr("Enter Multiplier: ", TmpStr);
ASCTOX(TmpStr, Multiplier);
XFLOOR(Multiplier, Tmp1);
if (XEQUAL(Multiplier, Tmp1))
{
break;
}
fprintf(stderr, "Multiplier is not integral: ");
/* -------------------------------- */
/* Print 5 Frac digits in Min Field */
/* -------------------------------- */
fPutXFrac(stderr, Multiplier, 5, 0);
fprintf(stderr, "\n");
}
}
else
{
for (;;)
{
GetStr("Enter First Multiplier: ", TmpStr);
ASCTOX(TmpStr, Multiplier);
XFLOOR(Multiplier, Tmp1);
if (XEQUAL(Multiplier, Tmp1))
{
break;
}
fprintf(stderr, "Multiplier is not integral: ");
/* -------------------------------- */
/* Print 5 Frac digits in Min Field */
/* -------------------------------- */
fPutXFrac(stderr, Multiplier, 5, 0);
fprintf(stderr, "\n");
}
fflush(NULL);fprintf(stderr, "%c", NewlineCh);
for (;;)
{
GetStr("Enter Second Multiplier: ", TmpStr);
ASCTOX(TmpStr, Multiplier + NE);
XFLOOR(Multiplier + NE, Tmp1);
if (XEQUAL(Multiplier + NE, Tmp1))
{
break;
}
fprintf(stderr, "Multiplier is not integral: ");
/* -------------------------------- */
/* Print 5 Frac digits in Min Field */
/* -------------------------------- */
fPutXFrac(stderr, Multiplier + NE, 5, 0);
fprintf(stderr, "\n");
}
}
fflush(NULL);fprintf(stderr, "%c", NewlineCh);
for (;;)
{
GetStr("Enter Modulus: ", TmpStr);
ASCTOX(TmpStr, Modulus);
if (XGTE(EZERO, Modulus))
{
fprintf(stderr, "Modulus (%s) Must be > 0\n", TmpStr);
continue;
}
XFLOOR(Modulus, Tmp1);
if (XEQUAL(Modulus, Tmp1))
{
break;
}
fprintf(stderr, "Modulus is not integral: ");
/* -------------------------------- */
/* Print 5 Frac digits in Min Field */
/* -------------------------------- */
fPutXFrac(stderr, Modulus, 5, 0);
fprintf(stderr, "\n");
}
if (*HowMany == 1)
{
GetGCD(Modulus, Multiplier, Tmp1);
RelPrime = (short) (XEQUAL(Tmp1, EONE) &&
!XEQUAL(Multiplier, EONE) &&
!XEQUAL(Modulus, EONE));
if (!RelPrime)
{
fprintf(stderr,
"\nMultiplier and modulus not relatively prime.\n");
fprintf(stderr, "GCD of Multiplier and Modulus = ");
fPutXInt(stderr, Tmp1, 0); /* Print GCD in Min field */
fprintf(stderr, "\n"); fflush(stderr);
}
InputOK = (short)(InputOK && RelPrime);
}
else
{
GetGCD(Modulus, Multiplier, Tmp1);
RelPrime = (short) (XEQUAL(Tmp1, EONE) &&
!XEQUAL(Multiplier, EONE) &&
!XEQUAL(Modulus, EONE));
if (!RelPrime)
{
fprintf(stderr,
"\nFirst Multiplier and modulus not relatively prime.\n");
fprintf(stderr, "GCD of First Multiplier and Modulus = ");
fPutXInt(stderr, Tmp1, 0); /* Print GCD in Min field */
fprintf(stderr, "\n"); fflush(stderr);
}
InputOK = (short)(InputOK && RelPrime);
GetGCD(Modulus, Multiplier+NE, Tmp1);
RelPrime = (short) (XEQUAL(Tmp1, EONE) &&
!XEQUAL(Multiplier+NE, EONE) &&
!XEQUAL(Modulus, EONE));
if (!RelPrime)
{
fprintf(stderr,
"Second Multiplier and modulus not relatively prime.\n");
fprintf(stderr, "GCD of Second Multiplier and Modulus = ");
fPutXInt(stderr, Tmp1, 0); /* Print GCD in Min field */
fprintf(stderr, "\n"); fflush(stderr);
}
InputOK = (short)(InputOK && RelPrime);
}
fflush(NULL);fprintf(stderr, "%c", NewlineCh);
if (InputOK)
{
GetShort("Enter Maximum Dimension: ", Dimension);
fflush(stderr);
}
else
{
fprintf(stderr, "Error(s) detected in input data\n");
}
return(InputOK);
}